package org.aurora.module.user.infra.repository;

import com.mybatisflex.core.query.QueryCondition;
import org.aurora.module.user.domain.entity.User;
import org.aurora.module.user.domain.repository.UserRepository;
import org.aurora.module.user.infra.dal.mysql.converter.UserConverter;
import org.aurora.module.user.infra.dal.mysql.dataobject.UserDO;
import org.aurora.module.user.infra.dal.mysql.mapper.UserMapper;
import org.springframework.stereotype.Component;

import static org.aurora.module.user.infra.dal.mysql.table.UserTableDefined.USER;

/**
 * UserRepositoryImpl
 * <pre>
 * description: 用户仓储实现
 * create date: 2025-03-15 19:28:09
 * </pre>
 *
 * @author cao.yong
 */
@Component
public class UserRepositoryImpl implements UserRepository {

    private final UserMapper userMapper;

    public UserRepositoryImpl(UserMapper userMapper) {
        this.userMapper = userMapper;
    }

    @Override
    public void save(User entity) {
        this.userMapper.insert(UserConverter.INSTANCE.convert(entity));
    }

    @Override
    public void delete(Long id) {
        this.userMapper.deleteById(id);
    }

    @Override
    public void delete(User entity) {
        this.userMapper.delete(UserConverter.INSTANCE.convert(entity));
    }

    @Override
    public void update(User entity) {
        this.userMapper.update(UserConverter.INSTANCE.convert(entity));
    }

    @Override
    public User findById(Long id) {
        return UserConverter.INSTANCE.convert(this.userMapper.selectOneById(id));
    }

    @Override
    public User findByUsername(String username) {
        UserDO userDO = this.userMapper.selectOneByCondition(QueryCondition.create(USER.USERNAME, username));
        return UserConverter.INSTANCE.convert(userDO);
    }

    @Override
    public User findByTelephone(String telephone) {
        UserDO userDO = this.userMapper.selectOneByCondition(QueryCondition.create(USER.TELEPHONE, telephone));
        return UserConverter.INSTANCE.convert(userDO);
    }
}
